﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 图片识别
    /// 2019-2-20
    /// </summary>
    public partial class frmImage : Form
    {
        public frmImage()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            //打开计算机 找到相片
            this.openFileDialog1.ShowDialog();
            //显示图片到文本框
            this.txtImage.Text = this.openFileDialog1.FileName;

            var ApiKey = "XPD3zBrjVO8FsTD7RUzAaH5B";
            var SecretKey = "WCVnY2T6N2BCRCLgwtxLtTQdiiYPta8T";
            var tuPian = this.openFileDialog1.FileName;
            var client = new Baidu.Aip.Ocr.Ocr(ApiKey, SecretKey);
            var image = File.ReadAllBytes(tuPian);


            var result = client.GeneralBasic(image, null);
            this.textBox1.Text = result.ToString();
            

            JObject jo = (JObject)JsonConvert.DeserializeObject(result.ToString());
            string mes = jo["log_id"].ToString();
            string mes1 = jo["words_result_num"].ToString();

            JArray arr = (JArray)jo["words_result"];

           // string mes2 = jo["words_result"][0]["words"].ToString();
           StringBuilder  sb=new StringBuilder();
            foreach (JObject j in arr)
            {
                sb.AppendLine(j["words"].ToString());
                
            }
            this.textBox4.Text = sb.ToString();
            this.textBox2.Text = mes;
            this.textBox3.Text = mes1;
            

        }

    }
}
